package com.flex.core.model.map
{
	import mx.collections.ArrayCollection;
	/**
	 * @author sam
	 */ 
	public class HashMap
	{
		private var arrayCollection:ArrayCollection=null;

		public function HashMap()
		{
			this.arrayCollection=new ArrayCollection();
		}

		/**
		 * getMapSize
		 */ 
		public function getSize():int{
			return this.arrayCollection.length;
		}
		
		/**
		 * get Value from Map by Key
		 */ 
		public function gets(key:String):Object
		{
			for each (var obj:MapObject in this.arrayCollection)

			{

				if (obj.key == key)

				{
					return obj.value;

				}

			}

			return null;

		}
		
		/**
		 * get MapObject from Collection by Key
		 */ 
		private function getObjFromCollection(key:String):MapObject{
			for each (var obj:MapObject in this.arrayCollection)
			
			{
				
				if (obj.key == key)
					
				{
					return obj;
					
				}
				
			}
			
			return null;
		}

		/**
		 * put Value to Map with Key
		 */ 
		public function puts(key:String, value:Object):void
		{
			if (!this.arrayCollection.contains(value))
			{//if not exist just put to map
				this.arrayCollection.addItem(new MapObject(key, value));
			}else
			{//if exist , get mapObject from collection fisrt ,then replace value to new value
				getObjFromCollection(key).value = value;
			}
		}

		public function remove(key:String):Boolean
		{
			var mo:MapObject=null;
			for (var i:int=0; i < this.arrayCollection.length; ++i)
			{
				mo=MapObject(this.arrayCollection.getItemAt(i));
				if (mo.key == key)
				{
					this.arrayCollection.removeItemAt(i);
					return true;
				}

			}

			return false;

		}

		public function contains(key:String):Boolean

		{

			for each (var obj:MapObject in this.arrayCollection)

			{

				if (obj.key == key)

				{

					return true;

				}

			}

			return false;

		}

	}
}